package org.harlov.testsys.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;

import org.harlov.testsys.loger;
import org.harlov.testsys.db.suppdata.test_struct;


public class tests {
	
	private static db_main DB;
	
	private static ArrayList<test_struct> tests_list;
	private static DefaultListModel tests_views;
	
	public tests(db_main DB_m) {
		DB = DB_m;
		tests_list = new ArrayList<test_struct>();
		tests_views = new DefaultListModel();
		
	}
	
	
	
	public static DefaultListModel getTestsView(String creator) {
		getTests(creator);
		tests_views.clear();
		
		for (int i = 0 ; i<tests_list.size();i++) {
			test_struct temp_s = tests_list.get(i);
			tests_views.addElement(temp_s.getName());
			
		}
		return tests_views;
		
	}
	//Получени параметров конкретного теста
	
	public static int getTestId (int list_id) {
		test_struct temp_ts = tests_list.get(list_id-1);
		System.out.println(temp_ts);
		return temp_ts.getId();
	}
	
	public static int getTestTime (int list_id) {
		test_struct temp_ts = tests_list.get(list_id-1);
		return temp_ts.getTimeAv();
	}
	
	public static String getTestName (int list_id) {
		test_struct temp_ts = tests_list.get(list_id-1);
		return temp_ts.getName();
	}
	
	public static int getTestQCount(int test_id) {
		test_struct temp_ts = tests_list.get(test_id);
		return temp_ts.getQuestsCount();
	}
	
	public static DefaultListModel getTestsView(int theme,int diff) {
		getTests(theme,diff);
		tests_views.clear();
		for (int i = 0 ; i<tests_list.size();i++) {
			test_struct temp_s = tests_list.get(i);
			tests_views.addElement(temp_s.getName());
		}
		return tests_views;
		
	}
	
	public static ArrayList<test_struct> getTests(int theme,int diff) { //Выборка тестов по теме и сложности для студентов
		tests_list.clear();
		
		ResultSet res;
		try {
			System.out.println("SELECT * FROM tests WHERE theme='"+theme+"' AND complex='"+diff+"' ORDER BY id");
			res = DB.makeReq(  "SELECT * FROM tests WHERE theme='"+theme+"' AND complex='"+diff+"' ORDER BY id");
			test_struct temp ;
			while (res.next()) {
				temp = new test_struct(res.getInt("id"),res.getInt("theme"),res.getInt("complex"),res.getInt("creator"),res.getString("name"),res.getInt("time"),res.getInt("quest_count")); //ID
				tests_list.add(temp);
				System.out.print(res.getString("name"));
			}
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		return tests_list;
		
	}
	
	
	public static ArrayList<test_struct> getTests(String creator) { //Полная выборка тестов для определенного редактора
		tests_list.clear();
		int c_id =0 ;
		ResultSet res;
		try {
			c_id = users.getId(creator);
			res = DB.makeReq("SELECT * FROM tests WHERE creator='"+c_id+"' ORDER BY id");
			test_struct temp ;
			while (res.next()) {
				temp = new test_struct(res.getInt("id"),res.getInt("theme"),res.getInt("complex"),res.getInt("creator"),res.getString("name"),res.getInt("time"),res.getInt("quest_count")); //ID
				tests_list.add(temp);
				System.out.print(res.getString("name"));
			}
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		return tests_list;
		
	}
	
	
	
	public static int addTest(int theme,int diff,String creator,String name,int time_av ) {
		
		try {
			int creat_id = users.getId(creator);
			String [] prm = {Integer.toString(themes.getIdByList(theme)),Integer.toString(diff),Integer.toString(creat_id),name,Integer.toString(time_av)}; 
			String params = db_main.makeParams(prm);
			DB.makeUp("INSERT INTO tests (theme,complex,creator,name,time) VALUES ("+params+")");
			getTests(creator);
		
		}
		catch (SQLException e) {loger.add(e.getMessage(),null, 1);}
		
		return 0;
	} 
	
	public static int delTest(int test) {
		int res;
		try {
			DB.makeUp("DELETE FROM tests WHERE id='"+test+"'");
			
			
		}
		
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		
		return 0;
	}
	
	public static int saveTest (int id , int theme , int complex , String name , int time , int quest_count) {
	
		try {
			DB.makeUp("UPDATE tests SET theme = '"+themes.getIdByList(theme)+"',complex='"+complex+"',name='"+name+"',time='"+time+"',quest_count='"+quest_count+"' WHERE id='"+id+"'");
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		return 0;		
	}
	
	}

